import React from 'react';
import { parse } from 'qs';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export function isUrl(path) {
  return reg.test(path);
}

// 判断一个对象是否为空，
/*
null  true
undefined true
'' true
*/
export function isEmpty(obj) {
  if (obj === null) {
    return true
  }
  if (obj === '') {
    return true
  }
  if (obj === undefined) {
    return true
  }
  return false
}

// 1,2,3 转 A,B,C
/*
  1 为 A
*/
export function numberToChar(num) {
  if (num <= 26) {
    return String.fromCharCode(65 + parseInt(num))
  }
  return null;
}
/*
  fetch 只能用FormData
*/
export function paramsToFormData(params) {
  const formData = new FormData();
  for (let x in params) {
    formData.append(x, params[x]);
  }
  return formData;
}


/*
  删除对象内空属性
*/
export function removeProperty(object) {
  for (const prop in object) {
    if (object[prop] === '' || object[prop] === null || object[prop] === undefined) {
      object[prop] = ''
    }
  }
  // console.log("utils",object)
  return object;
}

export function getPageQuery() {
  return parse(window.location.href.split('?')[1]);
}